// ==========================================================================
// 云捷GO自动生成控制器相关代码，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：{{.table.CreateTime}}
// 生成路径: app/controller/{{.table.ModuleName}}/{{.table.BusinessName}}/{{.table.BusinessName}}_controller.go
// 生成人：{{.table.FunctionAuthor}}
// ==========================================================================
package {{.table.BusinessName}}

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"{{.table.PackageName}}/app/model"
	{{.table.BusinessName}}Model "{{.table.PackageName}}/app/model/{{.table.ModuleName}}/{{.table.BusinessName}}"
	{{.table.BusinessName}}Service "{{.table.PackageName}}/app/service/{{.table.ModuleName}}/{{.table.BusinessName}}"
	"{{.table.PackageName}}/app/service/utils/response"
)

//列表页
func List(r *ghttp.Request) {
	response.WriteTpl(r, "{{.table.ModuleName}}/{{.table.BusinessName}}/list.html")
}

//列表分页数据
func ListAjax(r *ghttp.Request) {
	var req *{{.table.BusinessName}}Model.SelectPageReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorMsg(r, "列表查询", req, model.Buniss_Other, err.Error())
	}
	rows := make([]{{.table.BusinessName}}Model.Entity, 0)
	result, page, err := {{.table.BusinessName}}Service.SelectListByPage(req)

	if err == nil && result != nil {
		rows = *result
	}

	r.Response.WriteJsonExit(model.TableDataInfo{
		Code:  0,
		Msg:   "操作成功",
		Total: page.Total,
		Rows:  rows,
	})
}

//新增页面
func Add(r *ghttp.Request) {
	response.WriteTpl(r, "{{.table.ModuleName}}/{{.table.BusinessName}}/add.html")
}

//新增页面保存
func AddSave(r *ghttp.Request) {
	var req *{{.table.BusinessName}}Model.AddReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorMsg(r, "新增{{.table.FunctionName}}", req, model.Buniss_Add, err.Error())
	}

	rid, err := {{.table.BusinessName}}Service.AddSave(req, r.Session)

	if err != nil || rid <= 0 {
		response.ErrorAdd(r, "新增{{.table.FunctionName}}", req)
	}
	response.SucessDataAdd(r, "新增{{.table.FunctionName}}", req, rid)
}

//修改页面
func Edit(r *ghttp.Request) {
	id := r.GetQueryInt64("id")

	if id <= 0 {
		response.WriteTpl(r, "error/error.html", g.Map{
			"desc": "参数错误",
		})
		return
	}

	entity, err := {{.table.BusinessName}}Service.SelectRecordById(id)

	if err != nil || entity == nil {
		response.WriteTpl(r, "error/error.html", g.Map{
			"desc": "数据不存在",
		})
		return
	}

	response.WriteTpl(r, "{{.table.ModuleName}}/{{.table.BusinessName}}/edit.html", g.Map{
		"{{.table.BusinessName}}": entity,
	})
}

//修改页面保存
func EditSave(r *ghttp.Request) {
	var req {{.table.BusinessName}}Model.EditReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorMsg(r, "{{.table.FunctionName}}岗位", req, model.Buniss_Add, err.Error())
	}

	rs, err := {{.table.BusinessName}}Service.EditSave(&req, r.Session)

	if err != nil || rs <= 0 {
		response.ErrorAdd(r, "修改{{.table.FunctionName}}", req)
	}
	response.SucessDataAdd(r, "修改{{.table.FunctionName}}", req, rs)
}

//删除数据
func Remove(r *ghttp.Request) {
	var req *model.RemoveReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorEdit(r, "删除{{.table.FunctionName}}", req)
	}

	rs := {{.table.BusinessName}}Service.DeleteRecordByIds(req.Ids)

	if rs > 0 {

		response.SucessDataDel(r, "删除{{.table.FunctionName}}", req, rs)
	} else {
		response.ErrorDataMsg(r, "删除{{.table.FunctionName}}", req, model.Buniss_Del, 0, "未删除数据")
	}
}

//导出
func Export(r *ghttp.Request) {
	var req *{{.table.BusinessName}}Model.SelectPageReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorOther(r, "导出Excel", req)
	}
	url, err := {{.table.BusinessName}}Service.Export(req)

	if err != nil {
		response.ErrorMsg(r, "导出Excel", req, model.Buniss_Other, err.Error())
	}

	response.SucessMsg(r, "导出Excel", req, model.Buniss_Other, url)
}